<template>
  <div class="title">我的收藏</div>
  <div>
    <el-table :data="collectList.list" >
      <el-table-column label="序号" prop="id" width="80"/>
      <el-table-column prop="pic" label="图片" width="100px">
        <template #default="scope">
          <el-image preview-teleported=true v-if="scope.row.goods.picList && scope.row.goods.picList.length > 0"
                    :src="SERVER_ADDR + '/goods/pic/' + scope.row.goods.picList[0].url"
                    :zoom-rate="1.2" :max-scale="7" :min-scale="0.2" :initial-index="1" fit="cover"
                    :preview-src-list="scope.row.goods.picList.map(pic => SERVER_ADDR + '/goods/pic/' + pic.url)"/>
        </template>
      </el-table-column>
      <el-table-column label="商品名称" prop="goods.name" />
      <el-table-column label="颜色" prop="goods.color" />
      <el-table-column label="版本" prop="goods.version" />
      <el-table-column label="价格" prop="goods.price" />
      <el-table-column label="操作">
        <template #default="scope">
          <el-button type="danger" size="small" @click="cancelCollect(scope.row.id)">取消收藏</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-row class="row-bg" justify="center" style="padding-top: 20px;">
      <el-form-item>
        <el-pagination background layout="prev, pager, next" :total="collectList.total"
                       :page-size="collectList.pageSize" @change="getCollectList"/>
      </el-form-item>
      <el-form-item>
        <el-select v-model="collectList.pageSize" placeholder="每页显示条数" style="width: 100px; padding-left: 40px"
                   @change="getCollectList(collectList.pageNum)">
          <el-option label="5条" :value="5"/>
          <el-option label="10条" :value="10"/>
          <el-option label="15条" :value="15"/>
        </el-select>
      </el-form-item>
    </el-row>
  </div>
</template>

<script setup>
import {ref} from "vue";
import CollectApi from "@/api/CollectApi.js";
import {ElMessage} from "element-plus";

const SERVER_ADDR = ref(import.meta.env.VITE_SERVER_ADDR)
const collectList = ref({
  page: null,
  pageNum: null,
  pageSize: 10
});

//获取当前用户的收藏
function getCollectList(pageNum) {
  CollectApi.selectByUserId(pageNum, collectList.value.pageSize)
      .then(resp => {
        collectList.value = resp.data;
      });
}
getCollectList(1);
//取消收藏
function cancelCollect(id) {
  CollectApi.delete(id)
      .then(resp => {
        if (resp.code == 10000) {
          ElMessage.success(resp.msg);
        } else {
          ElMessage.error(resp.msg);
        }
        getCollectList(1);
      });
}
</script>

<style scoped>
.title {
  margin-bottom: 20px;
  font-size: 20px;
  font-weight: bold;
  color: var(--theme-color);
}
</style>